import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import path from 'path'
import detectPort from 'detect-port'

const timeStamp = new Date().getTime()
const port = await detectPort(3000)
// const baseUrl = 'https://chitu.tianpingjiakeji.com/'
// const baseUrl = 'https://zcexapp.zhichu.tech/'
const baseUrl = 'http://121.229.202.28:452/'
// const baseUrl = 'https://zcexapp.zhichu.tech/'
function resolve (p) {
  return path.resolve(__dirname, p)
}


// https://vite.dev/config/
export default defineConfig({
  plugins: [
    react(),
  ],
  build: {
    rollupOptions: {
      // 输出配置
      output: {
        // 入口文件名
        entryFileNames: `assets/[name].${timeStamp}.js`,
        // 块文件名
        chunkFileNames: `assets/[name]-[hash].${timeStamp}.js`,
        // 资源文件名 css 图片等等
        assetFileNames: `assets/[name]-[hash].${timeStamp}.[ext]`,
      }
    },
  },
  server: {
    port,
    proxy: {
      '/api/': {
        target: baseUrl,
        changeOrigin: true,
        // rewrite: path => path.replace(/^\/api/, '')
      },
      '/upload/': {
        target: baseUrl,
        changeOrigin: true,
        // rewrite: path => path.replace(/^\/api/, '')
      },
      '/uploads/': {
        target: baseUrl,
        changeOrigin: true,
        // rewrite: path => path.replace(/^\/api/, '')
      },
      '/gpt/': {
        target: baseUrl,
        // target: 'http://opengptsapi.tianpingjiakeji.com',
        changeOrigin: true,
        // rewrite: path => path.replace(/^\/gpt/, '')
      }
    }
  },
  resolve: {
    alias: {
      '@src': resolve('src'),
      '@js': resolve('src/assets/js'),
      '@imgs': resolve('src/assets/imgs'),
      '@redux': resolve('src/redux'),
      '@router': resolve('src/router'),
      '@components': resolve('src/components'),
      '@hooks': resolve('src/hooks'),
      '@gpt': resolve('src/gpt'),
      '@pages': resolve('src/pages'),
      '@views': resolve('src/views'),
    }
  },
  css: {
    preprocessorOptions: {
      less: {
        javascriptEnabled: true
      }
    }
  }
})
